﻿using System.Windows;
using ImageAppDataModels;
using System.Windows.Data;
using ImageAppViewModels;
using BrainTechLLC;
using System;

namespace SettingsWindowsHandler.Plugin
{
    /// <summary>
    /// Interaction logic for SettingsWindow.xaml
    /// </summary>
    public partial class SettingsWindow : Window
    {
        public static readonly DependencyProperty SettingsViewModelProperty =
            DependencyProperty.Register("SettingsViewModel", typeof(SettingsViewModel), typeof(SettingsWindow), new PropertyMetadata(null));

        public SettingsViewModel SettingsViewModel
        {
            get { return (SettingsViewModel)GetValue(SettingsViewModelProperty); }
            set { SetValue(SettingsViewModelProperty, value); }
        }

        public SettingsWindow()
        {
            InitializeComponent();
        }

        void SettingsWindow_Loaded(object sender, RoutedEventArgs e)
        {
            Binding b = new Binding() { Source = SettingsViewModel, Mode = BindingMode.OneWay, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged };            
            settingsView.SetBinding(SettingsView.SettingsViewModelProperty, b);            
        }

        private void Window_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == System.Windows.Input.Key.Escape)
            {
                e.Handled = true;
                this.Close();
            }
            else if (e.Key == System.Windows.Input.Key.S &&
                    (((e.KeyboardDevice.Modifiers & System.Windows.Input.ModifierKeys.Control) == System.Windows.Input.ModifierKeys.Control) ||
                     ((e.KeyboardDevice.Modifiers & System.Windows.Input.ModifierKeys.Alt) == System.Windows.Input.ModifierKeys.Alt)))
            {
                e.Handled = true;
                SettingsWindowHandler.Handler.Handler.SettingsWatcher.SaveData();                
                this.Close();
            }
        }

        private void SaveSettings(object sender, System.Windows.Input.ExecutedRoutedEventArgs e)
        {
            SettingsWindowHandler.Handler.Handler.SettingsWatcher.SaveData();            
        }

        private void settingsView_RequestClose(object sender, System.EventArgs e)
        {
            this.Close();
        }
    }
}
